package com.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.JdUserMapper;
import com.pojo.JdUser;
import com.pojo.JdUserExample;
import com.vo.QueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    
    @Resource
    private JdUserMapper userMapper;
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public PageInfo<JdUser> queryByPage(Integer pageNum , Integer pageSize , QueryVo vo){
        JdUserExample example = new JdUserExample();
        JdUserExample.Criteria criteria = example.createCriteria();
        
        if (vo != null) {
            if (vo.getU_id() != null && !"".equals(vo.getU_id().trim())) {
                criteria.andU_idEqualTo(vo.getU_id().trim());
            }
            if (vo.getU_name() != null && !"".equals(vo.getU_name().trim())) {
                criteria.andU_nameEqualTo(vo.getU_name().trim());
            }
            if (vo.getU_phone() != null && !"".equals(vo.getU_phone().trim())) {
                criteria.andU_phoneEqualTo(vo.getU_phone().trim());
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        List<JdUser> list = userMapper.selectByExample(example);
        list.forEach(user -> System.out.println("\tuser : " + user));
        return new PageInfo<>(list);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public JdUser queryByKey(int u_num){
        return userMapper.selectByPrimaryKey(u_num);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public List<JdUser> queryByType(JdUser user){
        JdUserExample example = new JdUserExample();
        JdUserExample.Criteria criteria = example.createCriteria();
        if (user != null){
            if (user.getU_id() != null && !"".equals(user.getU_id().trim())){
                criteria.andU_idEqualTo(user.getU_id().trim());
            }
            if (user.getU_logInName() != null && !"".equals(user.getU_logInName().trim())) {
                criteria.andU_logInNameEqualTo(user.getU_logInName().trim());
            }
            if (user.getU_name() != null && !"".equals(user.getU_name().trim())) {
                criteria.andU_nameLike("%"+user.getU_name().trim()+"%");
            }
            if (user.getU_email() != null && !"".equals(user.getU_email().trim())) {
                criteria.andU_emailEqualTo(user.getU_email().trim());
            }
            if (user.getU_phone() != null && !"".equals(user.getU_phone().trim())) {
                criteria.andU_phoneEqualTo(user.getU_phone().trim());
            }
        }
        return userMapper.selectByExample(example);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int add(JdUser user){
        return userMapper.insertSelective(user);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int update(JdUser user){
        return userMapper.updateByPrimaryKeySelective(user);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int del(Integer u_num){
        return userMapper.deleteByPrimaryKey(u_num);
    }
    
}
